<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @author api.wes@gmail.com
 * @version 0.1
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

class SensapilityAccount extends SensapilityService{

  /**
   * @param soapclientnusoap $soapClient the NuSOAP object that communicates 
   * with the service
   * @param array $namespaces associative array that matches namespace labels
   * to namespace URIs
   * @param array $defaultNamespaceLabel the namespace to be used with most
   * tags
   * @return none
   */
  function SensapilityAccount($soapClient, 
                              $namespaces, 
                              $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient, 
                               $namespaces,
                               $defaultNamespaceLabel);
  }
  
  /**
   * Get all the syndication services and associated IDs for the current user
   * @return mixed Returns null if the call fails. Returns an array of
   * SyndicationService_Data objects
   */
  function getAllSyndicationServices() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getAllSyndicationServices',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }
  
  /**
   * @param string $service The name of the service to add to the user's account
   * @return SyndicationService_Data Returns null if the call fails, otherwise
   * returns the SyndicationService_Data object for the added service.
   */
  function addSyndicationService($service) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $valString = '<value>' . $service . '</value>';
    $callParameters = array($ns . ':synServiceType' => $valString);

    return parent::doSoapCall('addSyndicationService',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }
  
  /**
   * Associate with an existing account. The login email must be specified,
   * and one or both of the $postalCode and $phone must be supplied.
   * @param string $loginEmail The email address of the account you're trying
   * to associate with
   * @param string $postalCode The authentication hint 
   * @param string $phone The phone hint
   * @param string $developerUrl The site to be associated with this publisher
   * @return SyndicationService_Data[] An array of SyndicationService_Data 
   * objects for the services that this account is signed up for. If the call
   * fails null is returned.
   */
  function associateAccount($loginEmail, $postalCode, $phone, 
                                    $developerUrl) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':loginEmail' => $loginEmail,
                            $ns . ':postalCode' => $postalCode,
                            $ns . ':phone' => $phone,
                            $ns . ':developerUrl' => $developerUrl);

    return parent::doSoapCall('associateAccount',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }

  /**
   * Method to create a new AdSense account to be associated with your
   * developer account
   * @param string $loginEmail Email address of the publisher
   * @param string $entityType Either 'Individual' or 'Business'
   * @param string $websiteUrl The URL to register with the new account
   * @param string $websiteLocale The locale of the website.  This must be a
   * a Google supported locale.  See  
   * (http://code.google.com/apis/adsense/developer/adsense_api_locales.html)
   * for more information
   * @param string $usersPreferredLocale All correspondence with the user will 
   * occur in this locale. This must be a Google supported locale.
   * @param bool $emailPromotionsPreference Whether or not the publisher wants to receive
   * promotional emails from AdSense
   * @param array $synServiceTypes An array of strings for the services the
   * publisher wants to sign up for.
   * @param string $developerUrl Developer's URL to be associated with
   * publisher.
   * @return SyndicationService_Data[] An array of SyndicationService_Data 
   * objects for the services that this account is signed up for. If the call
   * fails null is returned.
   */
  function createAccount($loginEmail, 
                         $entityType, 
                         $websiteUrl, 
                         $websiteLocale, 
                         $usersPreferredLocale,
                         $emailPromotionsPreference, 
                         $synServiceTypes,
                         $developerUrl) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    if ( $emailPromotionsPreference ) {
      $emailPromotionsPreference = 'true';
    } else {
      $emailPromotionsPreference = 'false';
    }
    $entityArray = array('value' => $entityType);
    $callParameters = array($ns . ':loginEmail' => $loginEmail,
                            $ns . ':entityType' => array('value' => $entityArray),
                            $ns . ':websiteUrl' => $websiteUrl,
                            $ns . ':websiteLocale' => $websiteLocale,
                            $ns . ':usersPreferredLocale' => 
                                $usersPreferredLocale,
                            $ns . ':emailPromotionsPreference' => 
                                $emailPromotionsPreference);

    // since an array key can't have mutliple values, we have to do the 
    // syndication services array manually
    $parameterString = ArrayToXml($callParameters);
    $keys = array_keys($synServiceTypes);
    foreach ( $keys as $key ) {
      $parameterString .= '<' . $ns . ':synServiceTypes>' . 
                          '<value>' . 
                          $synServiceTypes[$key] . 
                          '</value>' . '</' . $ns . 
                          ':synServiceTypes>';
    }
    // handle developerUrl
    $parameterString .= '<' . $ns . ':developerUrl>' . $developerUrl .
                        '</' . $ns . ':developerUrl>';

    // depth is now 3 because of weird axis2 enumeration nesting
    return parent::doSoapCall('createAccount',
                              $parameterString,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }
  
  /**
   * @return string Whether or not the account application has been approved
   */
  function getApprovalStatus() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getApprovalStatus',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * @return string Returns the status of the association between this 
   * publisher account and your developer account
   */
  function getAssociationStatus() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getAssociationStatus',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }
  
  /**
   * @param string[] $pubIds The list of client ids to provide approval status for
   * @return PublisherApprovalStatus[] a list of PublisherApprovalStatus 
   * objects which connect the requested client id to the returned approval 
   * status. Client ids which have a failure condition will be omitted from 
   * the returned list. To obtain more specific information about why they 
   * failed, use the getApprovalStatus(int) api call.
   */
  function getBulkApprovalStatus($pubIds) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    // since we need multiple entries with the same key, create xml manually
    $parameterString = "";
    foreach ( $pubIds as $pubId ) {
      $parameterString .= '<' . $ns . ':clientIds>' . 
                          $pubId . 
                          '</' . $ns . ':clientIds>';
    }
    return parent::doSoapCall('getBulkApprovalStatus',
                              $parameterString,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }

  /**
   * @param string[] $pubIds The list of client ids to provide association 
   * status for.
   * @return PublisherAssociationStatus[] a list of PublisherAssociationStatus 
   * objects which connect the request client id to the returned association 
   * status. Client ids which have a failure condition will be omitted from 
   * the returned list. To obtain more specific information about why they 
   * failed, use the getAssociationStatus(int) api call.
   */
  function getBulkAssociationStatus($pubIds) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    // since we need multiple entries with the same key, create xml manually
    $parameterString = "";
    foreach ( $pubIds as $pubId ) {
      $parameterString .= '<' . $ns . ':clientIds>' . 
                          $pubId . 
                          '</' . $ns . ':clientIds>';
    }
    return parent::doSoapCall('getBulkAssociationStatus',
                              $parameterString,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }
  
  /**
  * @return DeveloperSiteData[] an array of DeveloperSiteData objects that 
  * contain the developer's site information.
  */
  function getDeveloperSiteData() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);
    return parent::doSoapCall('getDeveloperSiteData',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * @return string (true, false) Whether or not the publisher is currently opted-in to 
   * receive promotional email from Google AdSense
   */
  function getEmailPromotionsPreference(){
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getEmailPromotionsPreference',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * @return EntityType The entity type for this publisher's account
   */
  function getEntityType() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getEntityType',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * @param string $serviceType The service to get the id for
   * @return SyndicationService_Data object corresponding to the 
   * synServiceType parameter passed in. A SyndicationService_Data object 
   * contains a client ID and syndication type. 
   */
  function getSyndicationService($serviceType) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $valString = '<value>' . $serviceType . '</value>';
    $callParameters = array($ns . ':synServiceType' => $valString);
  
    return parent::doSoapCall('getSyndicationService',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }
  
  /**
   * @return string[] The list of supported website locales
   */
  function getSupportedWebsiteLocales() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);
  
    return parent::doSoapCall('getSupportedWebsiteLocales',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }
  
  /**
   * @param array synServiceTypes An array of strings for the services to check
   * @return string[] the list of supported website locales for a given type of SyndicationService.
   */
  function getSupportedWebsiteLocalesForSyndicationService($synServiceTypes) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $parameterString = "";
    foreach ( $synServiceTypes as $synServiceType ) {
      $parameterString .= '<' . $ns . ':synServiceTypes>' . 
                          '<value>' . 
                          $synServiceType . 
                          '</value>' . '</' . $ns . 
                          ':synServiceTypes>';
    }
  
    return parent::doSoapCall('getSupportedWebsiteLocalesForSyndicationService',
                              $parameterString,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * @return string The language used for communication with the publisher
   */
  function getUsersPreferredLocale() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getUsersPreferredLocale',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * @return string The language that the website is written in
   */
  function getWebsiteLocale() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getWebsiteLocale',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * @return string The URL for the registered website
   */
  function getWebsiteUrl() {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getWebsiteUrl',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }
  
  /**
   * @param string $developerUrl the developer site URL
   * @return none
   */
  function resendVerificationEmail($developerUrl) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':developerUrl' => $developerUrl);

    return parent::doSoapCall('resendVerificationEmail',
                                ArrayToXml($callParameters),
                                $this->__soapClient->namespaces[$ns],
                                false,
                                0,
                                true);
  }

  /**
   * @param bool $newPref Whether the publisher will receive emails from Google
   * AdSense
   * @return none
   */
  function setEmailPromotionsPreference($newPref) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    // PHP will convert boolean true to 1 and false to zero, so we convert to
    // the correct string value
    if ( $newPref ) {
      $newPref = "true";
    } else {
      $newPref = "false";
    }
    $callParameters = array($ns . ':enableEmailPromotions' => $newPref);

    return parent::doSoapCall('setEmailPromotionsPreference',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * @param string $newLocale The language to used for communications with the
   * user encoded as a Google locale, see
   * (http://code.google.com/apis/adsense/developer/adsense_api_lang_codes.html)
   *  for more information
   * @return none
   */
  function setUsersPreferredLocale($newLocale) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':usersPreferredLocale' => $newLocale);

    return parent::doSoapCall('setUsersPreferredLocale',
                                ArrayToXml($callParameters),
                                $this->__soapClient->namespaces[$ns],
                                false,
                                0,
                                true);
  }
}
?>
